﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmspitem : Form
    {
        public frmspitem()
        {
            InitializeComponent();
        }
        private int m_iItemId = 0;
        int spid = 0;
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        /// <summary>
        /// 获取商品编号
        /// </summary>
        /// <param name="itemid"></param>
        public frmspitem(int itemid)
        {
            InitializeComponent();
            spid = itemid;
            this.m_iItemId = itemid;
        }
        /// <summary>
        /// 生成简拼
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            txtjianpin.Text= PinyinHelper.GetShortPinyin(txtName.Text);
        }
        
        private void frmspitem_Load(object sender, EventArgs e)
        {
            spfl();
           // spid =Int32.Parse( txtbianma.Text);
            if (m_iItemId > 0)
            {
                txtkc.ReadOnly = true;
                jiazai();
            }
            else { }
        }
        /// <summary>
        /// 加载商品分类
        /// </summary>
        public void spfl()
        {
            string sql = "SELECT [ID],[type]FROM [商品分类]";
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds, "商品分类");
            cboType.DataSource = ds.Tables["商品分类"];
            cboType.DisplayMember = "type";
            conn.Close();
        }
        /// <summary>
        /// 加载商品信息
        /// </summary>
        public void jiazai()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = string.Format("SELECT [iid] ,[iname],[ifprice] ,[iuprice],[inprice],[iamount] ,[imoney] ,[icmoney] ,[itype] ,[iconvert] ,[ireadme] ,[istatus] ,[isbonus] ,[isticheng] ,[jc] ,[jianpin],商品分类.type,tizhong,hyj1,hyj2,hyj3  FROM [items] join 商品分类 on itype=商品分类.ID where iid='{0}'", m_iItemId);
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    txtbianma.Text = sdr["iid"].ToString();
                    txtName.Text = sdr["iname"].ToString();
                    txtjianpin.Text = sdr["jianpin"].ToString();
                    txtReadme.Text = sdr["Ireadme"].ToString();
                    txtjinhuojia.Text = sdr["ifprice"].ToString();
                    txtkc.Text = sdr["iamount"].ToString();
                    txtshoujia.Text = sdr["iuprice"].ToString();
                    txtjcdanjia.Text = sdr["inprice"].ToString();
                    cboType.Text = sdr["type"].ToString();
                    cboStatus.SelectedIndex =int.Parse( sdr["istatus"].ToString());
                    txthyj1.Text = sdr["hyj1"].ToString();
                    txthyj2.Text = sdr["hyj2"].ToString();
                    txthyj3.Text = sdr["hyj3"].ToString();
                    int jc = Int32.Parse(sdr["jc"].ToString());
                    
                    if (jc == 0)
                    {
                        ckjc.Checked = false;
                    }
                    else if (jc == 1)
                    {
                        ckjc.Checked = true;
                    }
                    int dz = Int32.Parse(sdr["Isbonus"].ToString());
                    if (dz == 0)
                    {
                        chkNoBonus.Checked = false;

                    }
                    else if (dz == 1)
                    {
                        chkNoBonus.Checked = true;
                    }
                    int tz = Int32.Parse(sdr["tizhong"].ToString());
                    if (tz == 1)
                    {
                        cbtz.Checked = true;
                    } else if (tz==0)
                    {
                        cbtz.Checked = false;
                    }
                }

            } catch { }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (chuanzhi.xiuzeng == 1)
            {
                if (txtbianma.Text != string.Empty)
                {
                    upsp();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("填写商品编码");
                }
            }
            else
            {
                if (txtbianma.Text != string.Empty)
                {
                    xinzeng();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("填写商品编码");
                }
            }
                
        }
        /// <summary>
        /// 新增商品
        /// </summary>
        public void xinzeng()
        {
            int fl = 0;
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql1 = string.Format("SELECT [ID],[type] FROM [BHairDB].[dbo].[商品分类] where type='{0}'", cboType.Text);
            SqlCommand comm1 = new SqlCommand(sql1, conn1);
            SqlDataReader sdr = comm1.ExecuteReader();
            while (sdr.Read())
            {
                fl = Int32.Parse(sdr["ID"].ToString());
            }
            int dz = 0;
            if (chkNoBonus.Checked == true)
            {
                dz = 1;
            }
            else if (chkNoBonus.Checked == false)
            {
                dz = 0;
            }
            int jc = 0;
            if (ckjc.Checked == true)
            {
                jc = 1;
            }
            if (ckjc.Checked == false)
            {
                jc = 0;
            }
            int tz = 0;
            if (cbtz.Checked == true)
            {
                tz = 1;
            }
            else if(cbtz.Checked==false)
            {
                tz = 0;
            }
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "INSERT INTO [items] ([iid] ,[iname]  ,[ifprice],[iuprice],[inprice] ,[iamount] ,[imoney],[icmoney],[itype],[iconvert] ,[ireadme] ,[istatus] ,[isbonus] ,[isticheng] ,[jc],[jianpin],tizhong,hyj1,hyj2,hyj3) VALUES ({0} ,'{1}','{2}','{3}' ,'{4}' ,'{5}' ,'0.00' ,'0.00' ,{6},0,'{7}' ,0 ,'{8}' ,0 ,{9} ,'{10}',{11},'{12}','{13}','{14}')",txtbianma.Text,txtName.Text,txtjinhuojia.Text,txtshoujia.Text,txtjcdanjia.Text,txtkc.Text,fl,txtReadme.Text,dz,jc,txtjianpin.Text,tz,txthyj1.Text,txthyj2.Text,txthyj3.Text);
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        public void upsp()
        {
            int fl = 0;
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql1 =string.Format( "SELECT [ID],[type] FROM [BHairDB].[dbo].[商品分类] where type='{0}'",cboType.Text);
            SqlCommand comm1 = new SqlCommand(sql1,conn1);
            SqlDataReader sdr = comm1.ExecuteReader();
            while (sdr.Read())
            {
                fl =Int32.Parse( sdr["ID"].ToString());
            }

            int dz = 0;
            if (chkNoBonus.Checked == true)
            {
                dz = 1;
            } else if (chkNoBonus.Checked==false)
            {
                dz = 0;
            }
            int jc = 0;
            if (ckjc.Checked == true)
            {
                jc = 1;
            }
            if (ckjc.Checked == false)
            {
                jc = 0;
            }
            int tz = 0;
            if (cbtz.Checked == true)
            {
                tz = 1;
            } else if (cbtz.Checked== false)
            {
                tz = 0;
            }
            int cy = 0;
            if (cbcy.Checked == true)
            {
                cy = 1;
            }
            else if(cbcy.Checked==false)
            {
                cy = 0;
            }
            string sql = string.Format("UPDATE [items] SET [iid] = {0} ,[iname] = '{1}' ,[ifprice] = '{2}' ,[iuprice] = '{3}' ,[inprice] = '{4}' ,[itype] = {5} ,[ireadme] = '{6}' ,[istatus] = {7} ,[isbonus] = {8} ,[jc] = {9} ,[jianpin] = '{10}',tizhong={11},cy={12},hyj1='{13}',hyj2='{14}',hyj3='{15}' WHERE iid={16}", txtbianma.Text, txtName.Text, txtjinhuojia.Text, txtshoujia.Text, txtjcdanjia.Text, fl,txtReadme.Text,cboStatus.SelectedIndex,dz,jc,txtjianpin.Text,tz,cy,txthyj1.Text,txthyj2.Text,txthyj3.Text,spid);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
            //修改所有单据包含此商品的名字
            SqlConnection connup = new SqlConnection(constr);
            connup.Open();
            //修改csmx表
           string sqlcsmx =string.Format( "UPDATE [csmx] SET [jcxmname] ='{0}' WHERE [jcspid]='{1}'",txtName.Text,txtbianma.Text);
            SqlCommand commcsmx = new SqlCommand(sqlcsmx,connup);
            commcsmx.ExecuteNonQuery();
            //修改jcxf表
            string sqljcxf = string.Format("UPDATE [jcxf] SET [xmname] ='{0}' WHERE [xmid] ='{1}'",txtName.Text,txtbianma.Text);
            SqlCommand commjcxf = new SqlCommand(sqljcxf,connup);
            commjcxf.ExecuteNonQuery();
            connup.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ckjc_CheckedChanged(object sender, EventArgs e)
        {
            if (ckjc.Checked == true)
            {
                label2.Visible = true;
                txtjcdanjia.Visible = true;
            }
            else
            {
                label2.Visible = false;
                txtjcdanjia.Visible = false;
            }
        }
    }
}
